﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DistinctCaptchaTest.DistinctCaptchaService;

namespace DistinctCaptchaTest
{
    public partial class TestForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GenerateImage();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //Submit Form
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            GenerateImage();
        }

        private void GenerateImage()
        {
            CaptchaService captchaService = new CaptchaService();
            GenerateCaptcha generateCaptcha = captchaService.Generate();

            imgCaptcha.ImageUrl = generateCaptcha.ImageUrl;
            ViewState["Tokan"] = generateCaptcha.Tokan;
        }

        protected void vldCaptcha_ServerValidate(object source, ServerValidateEventArgs args)
        {
            CaptchaService captchaService = new CaptchaService();

            if (captchaService.Validate((string)ViewState["Tokan"], txtUserInput.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
}